package com.rd.student.ruanjian.class4.zhouzihe.day17;

import java.io.*;

public class TestCopy {
    public static void main(String[] args) throws Exception {
        File srcFile = new File("stream.txt");
        File destFile = new File("streamCopy.txt");
        copyFile(srcFile, destFile);
    }

    public static void copyFile(File srcFile, File destFile) throws IOException {
        if (destFile.exists()) {
            destFile.createNewFile();
        }
        //先从源文件中读取数据，再将数据写入到目标文件中
        InputStream is = new FileInputStream(srcFile);
        OutputStream os = new FileOutputStream(destFile);
        //往往不按照文件的大小来创建字节数组
//        byte[] byteArr = new byte[(int) srcFile.length()];
        byte[] byteArr = new byte[1024];
        int len = 0;
        while ((len = is.read(byteArr)) != -1) {
            os.write(byteArr, 0, len);
        }

        //如果去掉额外的数组声明 也去掉对String的处理
        //相当于将读取数据和写入数据两个水管相连
        os.close();
        is.close();
    }
}
